package com.spring.beans.factory.support;

import com.spring.beans.BeansException;
import com.spring.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy{

    // 根据无参构造创建bean
    @Override
    public Object instantiate(BeanDefinition beanDefinition) throws BeansException {
        Class beanClass = beanDefinition.getBeanClass();
        try {
            Constructor declaredConstructor = beanClass.getDeclaredConstructor();
            return declaredConstructor.newInstance();
        }catch (Exception e){
            throw new BeansException("SimpleInstantiationStrategy failed to init bean!");
        }
    }

}
